<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-input
          size="default"
          v-model="state.tableData.param.mfdata"
          placeholder="请输入模型名称/供应商"
          style="max-width: 180px"
        >
        </el-input>
        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
        <el-button
          size="default"
          type="success"
          class="ml10"
          @click="onOpenAddLLM('add')"
        >
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增模型
        </el-button>
      </div>

      <el-table
        :data="state.tableData.data"
        v-loading="state.tableData.loading"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column
          prop="provider_name"
          label="供应商"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="llm_model_name"
          label="模型名称"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="llm_model_type"
          label="模型类型"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="base_url"
          label="基础地址"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="top_k"
          label="top_k"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="top_p"
          label="top_p"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="max_length"
          label="最大长度"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button
              size="small"
              text
              type="primary"
              @click="onOpenEditLLM('edit', scope.row)"
              >修改</el-button
            >
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="onHandleSizeChange"
        @current-change="onHandleCurrentChange"
        class="mt15"
        :pager-count="5"
        :page-sizes="[10, 20, 30]"
        v-model:current-page="state.tableData.param.pageNum"
        background
        v-model:page-size="state.tableData.param.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>

    <AILLMDialog ref="llmDialogRef" @refresh="getTableData()" />
  </div>
</template>
<script setup lang="ts">
import { defineAsyncComponent, reactive, onMounted, ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { getAPI } from "/@/utils/axios-utils";
import { LLMApi } from "/@/api-services/api";
import { AILLMOut } from "/@/api-services/models";

// 引入组件
const AILLMDialog = defineAsyncComponent(
  () => import("/@/views/system/aiLLM/llmEdit.vue")
);
const llmDialogRef = ref();
const state = reactive({
  tableData: {
    data: [] as Array<AILLMOut>,
    total: 0,
    loading: false,
    param: {
      mfdata: "",
      pageNum: 1,
      pageSize: 10,
    },
  },
});

const getTableData = async () => {
  state.tableData.loading = true;
  var res = await getAPI(LLMApi).listApiV1LlmListPost(state.tableData.param);

  state.tableData.data = res.data.data ?? [];
  state.tableData.total = res.data.count;
  setTimeout(() => {
    state.tableData.loading = false;
  }, 500);
};

const onOpenAddLLM = (type: string) => {
  llmDialogRef.value.openDialog(type);
};

const onOpenEditLLM = (type: string, row: RowUserType) => {
  llmDialogRef.value.openDialog(type, row);
};

const onRowDel = (row: AILLMOut) => {
  ElMessageBox.confirm(
    `此操作将永久删除模型名称：“${row.llm_model_name}”，是否继续?`,
    "提示",
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(async () => {
      let params = { id: row.id };
      var res = await getAPI(LLMApi).deleteApiV1LlmDelPost(params);
      if (res.data.code == 0) {
        getTableData();
      } else {
        ElMessage.success(res.data.message);
      }
    })
    .catch(() => {});
};

// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;
    .el-table {
      flex: 1;
    }
  }
}
</style>
